<template>
  <view>
	<view style="font-weight: 600; word-break: break-all; justify-content: flex-start; margin-top: 30rpx;margin-bottom: 22rpx;" :key="item.key">{{$t(item.label)}}</view>
      
	  <block v-if="((item.rules)&&(item.rules.start))">
		  
		  <block v-if="plat!='web'">
			  <picker mode="date" :value="dateText" v-model="show" :start="minDate" :end="endDate" @change="changeDate" :confirm-text="$t('确认')"
			  :cancel-text="$t('取消')">

			  			  <view class="date" style="height: 70rpx;position: relative;">
							  <view style="position: absolute;width: 100%;height:100%;left:0;top:0;z-index: 9;"></view>

			  			        <input v-model="dateText"
			  							 style="font-size: 36rpx;color: #c76e21;flex: 1;"
			  			        		 :placeholder="lang=='ru'?districts($t(item.placeholder)):$t(item.placeholder)"
			  			        		 input-align="left"
			  			        		 disabled
			  			        		 @click="show = true"/>
			  			    </view>
			  		  </picker>
		  </block>
		  <block v-if="plat=='web'">
				<u-picker
						v-model="show"
						:minDate="minDate"
				        :params="params"
				        mode="time"
				        :confirm-text="$t('确认')"
				        :cancel-text="$t('取消')"
				        :show-time-tag="false"
				        @confirm="confirm"/>
						
				<u-input v-model="dateText"
				:custom-style="myStyle"
						 :placeholder="lang=='ru'?districts($t(item.placeholder)):$t(item.placeholder)"
						 input-align="left"
						 disabled
						 @click="show = true"/>
		 </block>
		 
	  
	  
	  </block>
	  
	  
	  
	  <block v-else>
			<u-picker 
					v-model="show"
					:minDate="minDate"
		            :params="params"
		            mode="time"
		            :confirm-text="$t('确认')"
		            :cancel-text="$t('取消')"
		            :show-time-tag="false"
		            @confirm="confirm"/>
					
			<u-input v-model="dateText"
			:custom-style="myStyle"
					 :placeholder="lang=='ru'?districts($t(item.placeholder)):$t(item.placeholder)"
					 input-align="left"
					 disabled
					 @click="show = true"/>
	  </block>
	  
      <view style="border-bottom: 1px rgb(228, 231, 237) solid;margin-top: 22rpx;"></view>

  </view>
</template>

<script>
let time = new Date()
let year = time.getFullYear()
let month = (time.getMonth() + 1) < 10 ? "0" + (time.getMonth() + 1) : (time.getMonth() + 1)
let month1 = (time.getMonth()) < 10 ? "0" + (time.getMonth()) : (time.getMonth())
let day = (time.getDate()) < 10 ? "0" + (time.getDate()) : (time.getDate())


function getDate(type) {
    const date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
 
    if (type === 'start') {
        year = year - 10;
    } else if (type === 'end') {
        year = year + 10;
    }
    month = month > 9 ? month : '0' + month;
    day = day > 9 ? day : '0' + day;
 
    return `${year}-${month}-${day}`; 
}
export default {
  props: {
    item: {
      type: Object,
      default: {},
      required: true
    },
	postjhsj: {
	  type: String,
	  default: '',
	},
	postjiaohuo: {
	  type: [Array, Object, Number, String],
	  default: ''
	}
  },
  data() {
    return {
		myStyle: {
			textAlign: 'left',
			pointerEvents: "none"
		},
		lang: getApp().globalData.lang,
		plat:uni.getStorageSync('plat'),
		show: false,
		dateText: '',
		params: {
			year: true,
			month: true,
			day: true,
			hour: false,
			minute: false,
			second: false,
			// province: true,
			// city: true,
			// area: true,
			timestamp: true,
		},
		getToday: {
			year : new Date().getFullYear(),
			month : new Date().getMonth() + 1,
			day : new Date().getDate()
		},
		minDate:new Date().getFullYear()+'-'+(new Date().getMonth() + 1)+'-'+new Date().getDate(),
		endDate: getDate('end'), //可选择的最大日期

    };
  },
  mounted() {
    this.setDefaultValue()
	// console.log(this.minDate);
	if(this.postjhsj){
		// this.dateText=this.postjhsj
		this.dateText=new Date().toISOString().slice(0, 10);
	}
	if(this.postjiaohuo){
		this.dateText=new Date().toISOString().slice(0, 10);
	}
  },
  methods: {
	changeDate(e){
		this.dateText = e.detail.value;    
		this.$emit('change', this.item.key, this.dateText)
		if(this.dateText){
			this.item.placeholder = ''
		}
		
		console.log('选择的日期：',this.dateText)
	},
    districts(val) {
		return val.length > 10 ? val.slice(0, 23) + "...>" : val
	},
    setDefaultValue() {
      if (this.item.value && this.item.value !== '') {
        // this.dateText = this.$u.timeFormat(this.item.value, 'yyyy-mm-dd')
        this.dateText = this.item.value
      }
    },
    confirm(e) {
		this.dateText = e.year + '-' + e.month + '-' + e.day
		
			this.item.placeholder = ''
		
		var timestamp = new Date(this.dateText).getTime(); // 将字符串转为 Date 对象后获取其时间戳
		var timeday = new Date(this.minDate).getTime(); // 将字符串转为 Date 对象后获取其时间戳

		if((this.item.rules)&&(this.item.rules.start)){
			if(Number(timestamp)<Number(timeday)){
				uni.showModal({
					content:this.$t('发货时间不能小于今天哦'),
					cancelText: this.$t('否'),
					confirmText: this.$t('是'),
				})	  
				this.dateText = ""
				return false;
			}
		}


		this.$emit('change', this.item.key, this.dateText)
		// this.$emit('change', this.item.key, e.timestamp)
    }
  }
}
</script>

<style lang="scss">

</style>
